<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Car;
use DB;
use App\Carmodel;
use App\Tocar;
use Redirect;




class HomeController extends Controller
{

    public function addcar(Request $req){
       if ($req->all()!= null) {

            $cars = new Tocar();
            $cars->p_brand = $req->p_brand;
            $cars->p_subbrand = $req->p_subbrand;
            $cars->p_subsubbrand = $req->p_subsubbrand;
            $cars->p_price = $req->p_price;
            $cars->p_color = $req->p_color;
            $cars->p_model = $req->p_model;
            $cars->p_gas = $req->p_gas;
            $cars->p_transmission = $req->p_transmission;
            $cars->p_kilometre = $req->p_kilometre;
            $cars->p_details = $req->p_details;
            $cars->p_username = $req->p_username;
            $cars->p_tel = $req->p_tel;
            if ($cars->save()) {
                 return Redirect::to('/'); 

            }else{

                 return Redirect::to('home/addcar');
            }
       }else{
        
        $carmodel = new Carmodel();
        $model = $carmodel->get();
        $cars = DB::table('brands')->where('b_parent', '-1')->get();
         return view('home/add_cars',['cars'=>$cars,'model'=>$model]);
    }

    }

    //Ajax父级栏目请求地址

    public function getAttributeByType(){
        $bid = $_GET['b_id'];
        $carss = DB::table('brands')->where('b_id',$bid)->first();
        $b_id = $carss->b_id;
        $cha = DB::table('brands')->where('b_parent',$b_id)->get();
        echo json_encode($cha);
    }
    //Ajax子级栏目请求地址
     public function songetAttributeByType(){
        // return 123;
        $bid = $_GET['b_id'];
        $carss = DB::table('brands')->where('b_id',$bid)->first();
        $b_id = $carss->b_id;
        $cha = DB::table('brands')->where('b_parent',$b_id)->get();
        echo json_encode($cha);
    }

}
